
import { defineStore } from 'pinia'
import request from '/@/utils/request'

interface DictState {
  cache: any
}
export const useDictStore = defineStore('app-dict', {
  // 推荐使用 完整类型推断的箭头函数
  state: (): DictState => {
    return {
      cache: {}
    }
  },
  actions: {
    /**
     * 请求数据
     * @param code 
     */
    requestData(code: any){
      if(this.cache[code]) return;
      this.cache[code] = []
      request.post('/sys/dict/getByDictType',{
        dictType: code
      }).then((res)=>{
        this.cache[code] = res.data
      }).catch(()=>{
        delete this.cache[code]
      })
    }
  },
  getters: {
    /**
     * 获取数据
     * @returns 
     */
   getDictData(): Function {
     return (code:any)=>{
      return this.cache[code]
     }
   }
  }
})